﻿using System;
using UFIDA.U9.WSS.ProxyService.Exceptions;
using UFIDA.UBF.SystemManage;
using UFSoft.UBF.Services.Session;
using UFSoft.UBF.Util.Context;
using Organization = UFIDA.U9.Base.Organization.Organization;

namespace UFIDA.U9.WSS.ProxyService.Models
{
    /// <summary>
    ///     上下文对象
    /// </summary>
    public class SessionObject : IDisposable
    {
        public const string PlatformContextExtend_DisableCheckOneProxyEnabled = "__WSS_DisableCheckOneProxyEnabled";

        public SessionObject(IEnterprise enterprise, string culture, string supportCultureNameList)
        {
            if (enterprise == null)
                throw new WSException(ExceptionCodes.ContextEnterpriseNull, "enterprise is null");
            //初始化Session
            InitSession();
            //设置上下文
            using (new SystemWritablePolicy())
            {
                PlatformContext.Current.EnterpriseID = enterprise.Code;
                PlatformContext.Current.EnterpriseName = enterprise.Name;
                PlatformContext.Current[PlatformContext.DEFAULT_CULTURE] = culture;
                PlatformContext.Current[PlatformContext.CULTURE] = culture;
                PlatformContext.Current[PlatformContext.SUPPORT_CULTURENAME_LIST] = supportCultureNameList;
                PlatformContext.Current[PlatformContext.DATETIME] = DateTime.Now;
            }
        }

        public SessionObject(ContextInfo contextInfo)
        {
            if (contextInfo == null)
                throw new WSException(ExceptionCodes.ContextInfoNull, "contextInfo is null");
            //初始化Session
            InitSession();
            //设置上下文
            SetContext(contextInfo);
        }

        public void Dispose()
        {
            //清空上下文
            ClearSession();
        }

        /// <summary>
        ///     初始化Session
        /// </summary>
        public void InitSession()
        {
            if (ServiceSession.Context != null && ServiceSession.Context.Operator == this) return;
            ServiceSession.InitinalSession(null);
            ServiceSession.EnterOperator(this);
        }

        /// <summary>
        ///     设置上下文
        /// </summary>
        /// <param name="contextInfo"></param>
        public static void SetContext(ContextInfo contextInfo)
        {
            using (new SystemWritablePolicy())
            {
                PlatformContext.Current[PlatformContext.ENTERPRISE_ID] = contextInfo.EnterpriseID;
                PlatformContext.Current[PlatformContext.ENTERPRISE_NAME] = contextInfo.EnterpriseName;
                PlatformContext.Current[PlatformContext.ORG_ID] = contextInfo.OrgID;
                PlatformContext.Current[PlatformContext.LOGINORG_ID] = contextInfo.OrgID;
                PlatformContext.Current[PlatformContext.OPERATINGORG_ID] = contextInfo.OrgID;
                PlatformContext.Current[PlatformContext.ORG_CODE] = contextInfo.OrgCode;
                PlatformContext.Current[PlatformContext.ORG_NAME] = contextInfo.OrgName;
                PlatformContext.Current[PlatformContext.USER_ID] = contextInfo.UserID;
                PlatformContext.Current[PlatformContext.USER_CODE] = contextInfo.UserCode;
                PlatformContext.Current[PlatformContext.USER_NAME] = contextInfo.UserName;
                PlatformContext.Current[PlatformContext.DEFAULT_CULTURE] = contextInfo.Culture;
                PlatformContext.Current[PlatformContext.CULTURE] = contextInfo.Culture;
                PlatformContext.Current[PlatformContext.SUPPORT_CULTURENAME_LIST] = contextInfo.SupportCultureNameList;
                PlatformContext.Current[PlatformContext.DATETIME] = DateTime.Now;
            }
        }

        /// <summary>
        ///     切换上下文组织
        /// </summary>
        /// <param name="org"></param>
        public static void SwitchOrg(Organization org)
        {
            using (new SystemWritablePolicy())
            {
                PlatformContext.Current[PlatformContext.ORG_ID] = org.ID;
                PlatformContext.Current[PlatformContext.LOGINORG_ID] = org.ID;
                PlatformContext.Current[PlatformContext.OPERATINGORG_ID] = org.ID;
                PlatformContext.Current[PlatformContext.ORG_CODE] = org.Code;
                PlatformContext.Current[PlatformContext.ORG_NAME] = org.Name;
            }
        }

        /// <summary>
        /// 禁止检查下一个代理有效
        /// 代理类调用前调用这个方法
        /// </summary>
        public static void DisableCheckOneProxyEnabled()
        {
            using (new SystemWritablePolicy())
            {
                PlatformContext.Current[PlatformContextExtend_DisableCheckOneProxyEnabled] = true;
            }
        }

        /// <summary>
        ///     清除Session
        /// </summary>
        public void ClearSession()
        {
            if (ServiceSession.Context == null || ServiceSession.Context.Operator != this) return;
            try
            {
                ServiceSession.Context.Dispose();
                ServiceSession.ExitOperator(this);
            }
            finally
            {
                ServiceSession.ReleaseSession();
            }
        }
    }
}